home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htscatchurl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-24  |  7.5 KB  |  278 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: URL catch .h                                           */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. /* Internal engine bytecode */
  38. #define HTS_INTERNAL_BYTECODE
  39.  
  40. // Fichier intercepteur d'URL .c
  41.  
  42. /* specific definitions */
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include "htsnet.h"
  46. #include "htslib.h"
  47. #include <fcntl.h>
  48. #if HTS_WIN
  49. #else
  50. #include <arpa/inet.h>
  51. #endif
  52. /* END specific definitions */
  53.  
  54. /* dΘfinitions globales */
  55. #include "htsglobal.h"
  56.  
  57. /* htslib */
  58. /*#include "htslib.h"*/
  59.  
  60. /* catch url */
  61. #include "htscatchurl.h"
  62.  
  63.  
  64. // URL Link catcher
  65.  
  66. // 0- Init the URL catcher with standard port
  67.  
  68. // catch_url_init(&port,&return_host);
  69. HTSEXT_API T_SOC catch_url_init_std(int* port_prox,char* adr_prox) {
  70.   T_SOC soc;
  71.   int try_to_listen_to[]={8080,3128,80,81,82,8081,3129,31337,0,-1};
  72.   int i=0;
  73.   do {
  74.     soc=catch_url_init(&try_to_listen_to[i],adr_prox);
  75.     *port_prox=try_to_listen_to[i];
  76.     i++;
  77.   } while( (soc == INVALID_SOCKET) && (try_to_listen_to[i]>=0));
  78.   return soc;
  79. }
  80.  
  81.  
  82. // 1- Init the URL catcher
  83.  
  84. // catch_url_init(&port,&return_host);
  85. HTSEXT_API T_SOC catch_url_init(int* port,char* adr) {
  86.   T_SOC soc = INVALID_SOCKET;
  87.   char h_loc[256+2];
  88.  
  89.   if (gethostname(h_loc,256)==0) {    // host name
  90.     SOCaddr server;
  91.     int server_size=sizeof(server);
  92.     t_hostent* hp_loc;
  93.     t_fullhostent buffer;
  94.  
  95.     // effacer structure
  96.     memset(&server, 0, sizeof(server));
  97.     
  98.     if ( (hp_loc=vxgethostbyname(h_loc, &buffer)) ) {  // notre host      
  99.  
  100.       // copie adresse
  101.       SOCaddr_copyaddr(server, server_size, hp_loc->h_addr_list[0], hp_loc->h_length);
  102.  
  103.       if ( (soc=socket(SOCaddr_sinfamily(server), SOCK_STREAM, 0)) != INVALID_SOCKET) {
  104.         SOCaddr_initport(server, *port);
  105.         if ( bind(soc,(struct sockaddr*) &server,server_size) == 0 ) {
  106.           SOCaddr server2;
  107.           int len;
  108.           len=sizeof(server2);
  109.           // effacer structure
  110.           memset(&server2, 0, sizeof(server2));
  111.           if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) {
  112.             *port=ntohs(SOCaddr_sinport(server));  // rΘcupΘrer port
  113.             if (listen(soc,10)>=0) {    // au pif le 10
  114.               SOCaddr_inetntoa(adr, 128, server2, len);
  115.             } else {
  116. #ifdef _WIN32
  117.               closesocket(soc);
  118. #else
  119.               close(soc);
  120. #endif
  121.               soc=INVALID_SOCKET;
  122.             }
  123.             
  124.             
  125.           } else {
  126. #ifdef _WIN32
  127.             closesocket(soc);
  128. #else
  129.             close(soc);
  130. #endif
  131.             soc=INVALID_SOCKET;
  132.           }
  133.           
  134.           
  135.         } else {
  136. #ifdef _WIN32
  137.           closesocket(soc);
  138. #else
  139.           close(soc);
  140. #endif
  141.           soc=INVALID_SOCKET;
  142.         }
  143.       }
  144.     }
  145.   }
  146.   return soc;
  147. }
  148.  
  149. // 2 - Wait for URL
  150.  
  151. // catch_url
  152. // returns 0 if error
  153. // url: buffer where URL must be stored - or ip:port in case of failure
  154. // data: 32Kb
  155. HTSEXT_API int catch_url(T_SOC soc,char* url,char* method,char* data) {
  156.   int retour=0;
  157.  
  158.   // connexion (accept)
  159.   if (soc != INVALID_SOCKET) {
  160.     T_SOC soc2;
  161.     struct sockaddr dummyaddr;
  162.     int dummylen = sizeof(struct sockaddr);  
  163.     while ( (soc2=accept(soc,&dummyaddr,&dummylen)) == INVALID_SOCKET);
  164.   /*
  165. #ifdef _WIN32
  166.     closesocket(soc);
  167. #else
  168.     close(soc);
  169. #endif
  170.   */
  171.     soc = soc2;
  172.     /* INFOS */
  173.     {
  174.       SOCaddr server2;
  175.       int len;
  176.       len=sizeof(server2);
  177.       // effacer structure
  178.       memset(&server2, 0, sizeof(server2));
  179.       if (getpeername(soc,(struct sockaddr*) &server2,&len) == 0) {
  180.         char dot[256+2];
  181.         SOCaddr_inetntoa(dot, 256, server2, sizeof(server2));
  182.         sprintf(url,"%s:%d", dot, htons(SOCaddr_sinport(server2)));  
  183.       }
  184.     }
  185.     /* INFOS */
  186.  
  187.     // rΘception
  188.     if (soc != INVALID_SOCKET) {
  189.       char line[1000];
  190.       char protocol[256];
  191.       line[0]=protocol[0]='\0';
  192.       //
  193.       socinput(soc,line,1000);
  194.       if (strnotempty(line)) {
  195.         if (sscanf(line,"%s %s %s",method,url,protocol) == 3) {
  196.           char url_adr[HTS_URLMAXSIZE*2];
  197.           char url_fil[HTS_URLMAXSIZE*2];
  198.           // mΘthode en majuscule
  199.           int i,r=0;
  200.           url_adr[0]=url_fil[0]='\0';
  201.           //
  202.           for(i=0;i<(int) strlen(method);i++) {
  203.             if ((method[i]>='a') && (method[i]<='z'))
  204.               method[i]-=('a'-'A');
  205.           }
  206.           // adresse du lien
  207.           if (ident_url_absolute(url,url_adr,url_fil)>=0) {
  208.             // Traitement des en-tΩtes
  209.             char loc[HTS_URLMAXSIZE*2];
  210.             htsblk blkretour;
  211.             memset(&blkretour, 0, sizeof(htsblk));    // effacer
  212.             blkretour.location=loc;    // si non nul, contiendra l'adresse vΘritable en cas de moved xx
  213.             // Lire en tΩtes restants
  214.             sprintf(data,"%s %s %s\r\n",method,url_fil,protocol);
  215.             while(strnotempty(line)) {
  216.               socinput(soc,line,1000);
  217.               treathead(NULL,NULL,NULL,&blkretour,line);  // traiter
  218.               strcatbuff(data,line);
  219.               strcatbuff(data,"\r\n");
  220.             }
  221.             // CR/LF final de l'en tΩte inutile car dΘja placΘ via la ligne vide juste au dessus
  222.             //strcatbuff(data,"\r\n");
  223.             if (blkretour.totalsize>0) {
  224.               int len=(int)min(blkretour.totalsize,32000);
  225.               int pos=strlen(data);
  226.               // Copier le reste (post Θventuel)
  227.               while((len>0) && ((r=recv(soc,(char*) data+pos,len,0))>0) ) {
  228.                 pos+=r;
  229.                 len-=r;
  230.                 data[pos]='\0';       // terminer par NULL
  231.               }
  232.             }
  233.             // Envoyer page
  234.             sprintf(line,CATCH_RESPONSE);
  235.             send(soc,line,strlen(line),0);
  236.             // OK!
  237.             retour=1;
  238.           }
  239.         }
  240.       }  // sinon erreur
  241.     }
  242.   }
  243.   if (soc != INVALID_SOCKET) {
  244. #ifdef _WIN32
  245.     closesocket(soc);
  246.     /*
  247.     WSACleanup();
  248.     */
  249. #else
  250.     close(soc);
  251. #endif
  252.   }
  253.   return retour;
  254. }
  255.  
  256.  
  257.  
  258. // Lecture de ligne sur socket
  259. void socinput(T_SOC soc,char* s,int max) {
  260.   int c;
  261.   int j=0;
  262.   do {
  263.     unsigned char b;
  264.     if (recv(soc,(char*) &b,1,0)==1) {
  265.       c=b;
  266.       switch(c) {
  267.         case 13: break;  // sauter CR
  268.         case 10: c=-1; break;
  269.         case 9: case 12: break;  // sauter ces caractΦres
  270.         default: s[j++]=(char) c; break;
  271.       }
  272.     } else
  273.       c=EOF;
  274.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  275.   s[j++]='\0';
  276. }
  277.  
  278.